/*
 *    This file is part of CeluMap.
 *
 *    CeluMap is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU Lesser General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    CeluMap is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    Copyright 2006, 2007, 2008 Gonzalo Merayo
 *    gonzalo (at) celumap.com.ar
 *    www.celumap.com.ar
 *
 */

package editor;

import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import db.DataSource;
import javax.swing.JButton;

public class MapBrowser extends JDialog {

	private static final long serialVersionUID = 1L;

	private JPanel jContentPane = null;

	private JList mapList = null;

	private JList versiones = null;

	private JMenuBar menu = null;

	private JMenu connection = null;

	private JMenu mapsMenu = null;

	private JMenuItem addVersionMenuItem = null;
	
	private DataSource ds = null;
	
	private Editor e = null;
	
	private Map<String, List<String>> maps = null;  //  @jve:decl-index=0:

	private JButton OK = null;

	/**
	 * @param owner
	 */
	public MapBrowser(Frame owner) {
		super(owner);
		initialize();
	}

	public MapBrowser(Editor owner, DataSource ds) {
		super(owner);
		initialize();
		e = owner;
		this.ds = ds;
		load();
		
	}

	private void load() {
		maps = ds.getMapList();
		final ArrayList<String> map_list = new ArrayList<String>(maps.keySet());
		mapList.setModel(new ListModel(){
			public void addListDataListener(ListDataListener l) {
				// TODO Auto-generated method stub
			}
			public Object getElementAt(int index) {
				return map_list.get(index);
			}
			public int getSize() {
				return map_list.size();
			}

			public void removeListDataListener(ListDataListener l) {
				// TODO Auto-generated method stub
			}});
		mapList.addListSelectionListener(new ListSelectionListener(){
			public void valueChanged(ListSelectionEvent e) {
				final ArrayList<String> versionList = new ArrayList<String>(maps.get(mapList.getSelectedValue()));	
				versiones.setModel(new ListModel(){
					public void addListDataListener(ListDataListener l) {
						// TODO Auto-generated method stub
					}
					public Object getElementAt(int index) {
						return versionList.get(index);
					}
					public int getSize() {
						return versionList.size();
					}
	
					public void removeListDataListener(ListDataListener l) {
						// TODO Auto-generated method stub
					}
				});
			}
		});
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(628, 290);
		this.setTitle("Select Map");
		this.setJMenuBar(getMenu());
		this.setContentPane(getJContentPane());
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.gridx = 2;
			gridBagConstraints11.gridy = 2;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.fill = GridBagConstraints.BOTH;
			gridBagConstraints1.gridx = 1;
			gridBagConstraints1.gridy = 1;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.weighty = 1.0;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.BOTH;
			gridBagConstraints.gridx = 2;
			gridBagConstraints.gridy = 1;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.weighty = 1.0;
			jContentPane = new JPanel();
			jContentPane.setLayout(new GridBagLayout());
			jContentPane.add(getMapList(), gridBagConstraints1);
			jContentPane.add(getVersiones(), gridBagConstraints);
			jContentPane.add(getOK(), gridBagConstraints11);
		}
		return jContentPane;
	}

	/**
	 * This method initializes mapList	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getMapList() {
		if (mapList == null) {
			mapList = new JList();
		}
		return mapList;
	}

	/**
	 * This method initializes versiones	
	 * 	
	 * @return javax.swing.JList	
	 */
	private JList getVersiones() {
		if (versiones == null) {
			versiones = new JList();
		}
		return versiones;
	}

	/**
	 * This method initializes menu	
	 * 	
	 * @return javax.swing.JMenuBar	
	 */
	private JMenuBar getMenu() {
		if (menu == null) {
			menu = new JMenuBar();
			menu.add(getConnection());
			menu.add(getMapsMenu());
		}
		return menu;
	}

	/**
	 * This method initializes connection	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getConnection() {
		if (connection == null) {
			connection = new JMenu();
			connection.setText("Connection");
		}
		return connection;
	}

	/**
	 * This method initializes mapsMenu	
	 * 	
	 * @return javax.swing.JMenu	
	 */
	private JMenu getMapsMenu() {
		if (mapsMenu == null) {
			mapsMenu = new JMenu();
			mapsMenu.setText("Maps");
			mapsMenu.add(getAddVersionMenuItem());
		}
		return mapsMenu;
	}

	/**
	 * This method initializes addVersionMenuItem	
	 * 	
	 * @return javax.swing.JMenuItem	
	 */
	private JMenuItem getAddVersionMenuItem() {
		if (addVersionMenuItem == null) {
			addVersionMenuItem = new JMenuItem();
			addVersionMenuItem.setText("Add Version");
			addVersionMenuItem.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e) {
					final String mapName = (String)mapList.getSelectedValue();
					final String version = (String)versiones.getSelectedValue();
					NewVersion v = new NewVersion(null, new ActionListener(){
						public void actionPerformed(ActionEvent e) {
							load();
						}}, mapName, version, ds, ds.getMapId(mapName, version));
					v.setVisible(true);
				}});
		}
		return addVersionMenuItem;
	}

	/**
	 * This method initializes OK	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getOK() {
		if (OK == null) {
			OK = new JButton();
			OK.setText("Open");
			OK.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent a) {
					String version = (String) versiones.getSelectedValue();
					String mapName= (String) mapList.getSelectedValue();
					if(version!=null && mapName!=null){
						ds.setVersion(version);
						ds.setMapName(mapName);
						dispose();
						e.loadMap();
					}
					
				}
			});
		}
		return OK;
	}

}  //  @jve:decl-index=0:visual-constraint="100,10"
